﻿Imports System.Data
Imports System.Data.SqlClient
Public Class TaiKhoanDAO
    Shared Function loadTaiKhoan() As DataTable
        Dim sql As String = "select * from TAIKHOAN"
        Dim dt As New DataTable
        dt = DataProvider.DataTable(sql)
        Return dt
    End Function

    Public Shared Sub themTaiKhoan(ByVal taik As TaiKhoanDTO)
        Dim sql As String = "insert into TAIKHOAN(TenTaiKhoan,LoaiTaiKhoan,HoVaTen,GioiTinh ,NgaySinh,DiaChi ,Email,TheLoaiYeuThich) values('" + _
            taik.tenTaiKhoan + "'," + taik.loaiTaiKhoan.ToString() + ",N'" + taik.hoVaTen + "',N'" + taik.gioiTinh + "','" + taik.ngaySinh.ToString() + _
            "',N'" + taik.diaChi + "','" + taik.email + "','" + taik.theLoaiYeuThich + "')"
        DataProvider.ExcuteQuery(sql)
    End Sub

    Public Shared Sub UpdateTK(ByVal taik As TaiKhoanDTO)

        Dim sql As String = "update TAIKHOAN set LoaiTaiKhoan= " + taik.loaiTaiKhoan.ToString() + ",HoVaTen=N'" + taik.hoVaTen + _
            "' ,GioiTinh= N'" + taik.gioiTinh + "',NgaySinh='" + taik.ngaySinh.ToString() + "' ,DiaChi=N'" + taik.diaChi + "' ,Email= '" + taik.email + _
            "', TheLoaiYeuThich= '" + taik.theLoaiYeuThich + "' where TenTaiKhoan='" + taik.tenTaiKhoan + "'"
        DataProvider.ExcuteQuery(sql)
    End Sub

    Public Shared Sub DeleteTK(ByVal taik As TaiKhoanDTO)
        Dim sql As String = "Delete from TAIKHOAN where TenTaiKhoan='" + taik.tenTaiKhoan + "'"
        DataProvider.ExcuteQuery(sql)
    End Sub

    Public Shared Function testTK(ByVal taik As TaiKhoanDTO) As Boolean
        Dim cn As New SqlConnection
        cn = DataProvider.CreaConnection()
        Dim cmd As New SqlCommand("sp_testTK", cn)
        cmd.CommandType = CommandType.StoredProcedure

        Dim input As New SqlParameter("@tentk", SqlDbType.Char)
        Dim output As New SqlParameter("@flag", SqlDbType.Bit)

        input.Size = 50
        input.Value = taik.tenTaiKhoan
        output.Direction = ParameterDirection.Output

        cmd.Parameters.Add(input)
        cmd.Parameters.Add(output)

        cmd.ExecuteNonQuery()

        Dim flag As Boolean
        flag = Convert.ToBoolean(output.Value)
        cn.Close()
        Return flag
    End Function

    Public Shared Function testEmail(ByVal taik As TaiKhoanDTO) As Boolean
        Dim cn As New SqlConnection
        cn = DataProvider.CreaConnection()
        Dim cmd As New SqlCommand("sp_testEmail", cn)
        cmd.CommandType = CommandType.StoredProcedure

        Dim input As New SqlParameter("@email", SqlDbType.Char)
        Dim output As New SqlParameter("@flag", SqlDbType.Bit)

        input.Size = 255
        input.Value = taik.email
        output.Direction = ParameterDirection.Output

        cmd.Parameters.Add(input)
        cmd.Parameters.Add(output)

        cmd.ExecuteNonQuery()

        Dim flag As Boolean
        flag = Convert.ToBoolean(output.Value)
        cn.Close()
        Return flag
    End Function


End Class
